package cn.mandifal.seller.controller;

import cn.mandifal.order.service.OrderService;
import cn.mandifal.pay.service.ZhiFuBaoPayService;
import cn.mandifal.pojo.DpOrderItem;
import com.alibaba.dubbo.config.annotation.Reference;
import entity.PageResult;
import entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 商家订单控制层
 * @author shun
 */
@RestController
@RequestMapping("/sellerOrderController")
public class SellerOrderController {
    @Reference(timeout = 3000)
    private OrderService orderService;
    @Reference(timeout = 3000)
    private ZhiFuBaoPayService zhiFuBaoPayService;

    /**
     * 模糊查询
     * @param dpOrderItem 订单详情实体
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @return
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody DpOrderItem dpOrderItem,int pageNum,int pageSize){
        System.out.println(dpOrderItem.getStatus());
        //设置此商户下的订单
        dpOrderItem.setSellerId(SecurityContextHolder.getContext().getAuthentication().getName());
        PageResult search = orderService.search(dpOrderItem, pageNum, pageSize);
        return search;
    }

    /**
     * 修改订单详情的状态
     * @param id 订单详情
     * @param status 目标状态
     * @return
     */
    @RequestMapping("/updateStatus")
    public Result updateStatus(String id,String status){
        try {
            orderService.updateStatus(id,status);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }
    /**
     * 商家确认退款
     * @param id 订单详情 id
     * @return
     */
    @RequestMapping("/sureRefund")
    public Result sureRefund(String id) {
        Map map=new HashMap();
        try {
            DpOrderItem oderItem = orderService.findOderItem(id);
            if (oderItem !=null){
                //设置支付宝流水号
                map.put("trade_no",oderItem.getTransactionId());
                //设置退款金额
                map.put("refund_amount",oderItem.getPrice()+"");
                //设置退款原因，写死
                map.put("refund_reason","不想要");
                //设置退款单号
                map.put("orderItemId",oderItem.getId());
            }
            //调用退款方法
            Boolean aBoolean = zhiFuBaoPayService.refund(map);
            if (aBoolean){
                //修改订单详情状态
                orderService.updateStatus((String) map.get("orderItemId"),"7");
                //返还库存
                orderService.returnRepertory((String) map.get("orderItemId"));
            }else {
                return Result.failed("退款失败1");
            }
            return Result.success("退款成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("退款失败2");
        }

    }

}
